package com.neeraj.SharingArtifacts;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TextTab extends ListActivity {
	private static final String DEBUG_TAG = "TEXT TAB";
	private static final int MEDIA_TYPE_TEXT = 4;
	private static TextTab instance;
	private ArrayList<String> child;
	private ListView listviewText;
	
	ArrayList<String> checkedTextItems;
	ArrayAdapter<String> fileList;
	DatabaseHelper dbHelper;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_layout);
        instance = this;
        listviewText = (ListView)findViewById(android.R.id.list);
        dbHelper=new DatabaseHelper(this);
		dbHelper.insertTypeData();
        showAllTextIDs();
    }
    
    public static TextTab getInstance() {
    	return instance;
    }
    
    void showAllTextIDs(){
    	
    	child = dbHelper.getTextIDs(MEDIA_TYPE_TEXT);    	
    	 
    	checkedTextItems = new ArrayList<String>();
    	
    	Log.i(DEBUG_TAG, child.toString());
    	
    	fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, child);
    	listviewText.setAdapter(fileList);
    	ListView list = getListView();
    	list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    	
    	listviewText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String item = (String) listviewText.getAdapter().getItem(arg2);
				CheckedTextView listItem = (CheckedTextView)arg1;
				if(!listItem.isChecked()){
					System.out.println(item + ":Status=" + dbHelper.isUploaded(item));
					if(!dbHelper.isUploaded(item)){
						Log.i(DEBUG_TAG, "Checked!!");
						checkedTextItems.add(item);
					}
				}
				else
					Log.d(DEBUG_TAG, "Not Checked!!");				
				Log.d(DEBUG_TAG, item);				
			}
		});
    }

    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	super.onBackPressed();
    	Toast.makeText(this, checkedTextItems.toString(), Toast.LENGTH_LONG).show();
    }
 
    void CatchError(String Exception) {	    	
    	Dialog diag=new Dialog(this);
    	diag.setTitle("Error!!!");
    	TextView txt=new TextView(this);
    	txt.setText(Exception);
    	diag.setContentView(txt);
    	diag.show();
    }
}